package com.zjw.service.message.impl;

import com.zjw.service.message.IEvent;
import com.zjw.service.message.IEventLooper;
import com.zjw.service.message.IConnectSession;

import java.util.ArrayList;
import java.util.List;

public class SimpleEventLooper implements IEventLooper {
    private final List<IEvent> connectSessionList = new ArrayList<>();

    @Override
    public void registerEvent(IEvent session) {
        connectSessionList.add(session);
    }

    @Override
    public void loop() {
        new Thread(() -> {
            while (true) {
                //simple select
                for (IEvent event : connectSessionList) {
                    if (event.hasEvent()) {
                        event.notifyEvent();
                    }
                }
            }
        }).start();
    }

    @Override
    public void unregisterEvent(IEvent session) {
        connectSessionList.remove(session);
    }
}
